package com.allwees.bs.module.oms.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.model.order.wf.refund.ERefundAction;
import com.allwees.bs.core.model.order.wf.refund.ERefundState;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.core.req.BranchUserQuery;
import com.allwees.bs.module.oms.entity.OrderRefund;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class OrderRefundQueryReq extends QueryReq<OrderRefund> implements BranchUserQuery<OrderRefund> {

    private String uuid;

    private String orderNo;

    private String defNo;

    /**
     * 所属店铺uuid
     */
    private String storeUuid;

    /**
     * sku商品uuid
     */
    private String orderItemUuid;

    private String productUuid;

    private ERefundStateType state;

    private List<String> stateStrList;

    /**
     * 退货阶段
     */
    private ERefundPhase refundPhase;

    private List<String> refundPhaseList;

    private String orderUuid;

    /**
     * 买家ID
     */
    private String userUuid;

    private Date createdAtFrom;

    private Date createdAtTo;

    private String retailerUuid;

    @Override
    public void buildQuery(QueryWrapper<OrderRefund> wrapper) {
        wrapper.and(StringUtils.isNotBlank(q), w -> {
            w.eq("uuid", q)
                    .or().eq("order_uuid", q)
                    .or().eq("order_no", q)
                    .or().eq("order_item_uuid", q);
        })
                .eq(StringUtils.isNotBlank(orderNo), "order_no", orderNo)
                .eq(StringUtils.isNotBlank(orderUuid), "order_uuid", orderUuid)
                .ge(createdAtFrom != null, "created_at", createdAtFrom)
                .le(createdAtTo != null, "created_at", createdAtTo);
        if(state != null) {
            //wrapper.eq("state", state.getStatus());

            stateStrList = Arrays.asList(state.getStatus());
        }

        if(refundPhase != null){
            //wrapper.in("latest_action", refundPhase.entityPhase());
            refundPhaseList =  refundPhase.entityPhase();
        } else {
            refundPhaseList =  Arrays.asList(
                    ERefundAction.USER_REQUEST_REFUND_ON_LIMIT.name(),
                    ERefundAction.USER_REQUEST_REFUND.name(),
                    ERefundAction.USER_CANCEL_REFUND.name(),
                    ERefundAction.RETAILER_CANCEL_SHIPPED_ON_LIMIT.name(),
                    ERefundAction.RETAILER_CANCEL_SHIPPED_AFTER_LIMIT.name(),
                    ERefundAction.PLATFORM_CANCEL_SHIPPED.name(),
                    ERefundAction.USER_SHIPPED.name(),
                    ERefundAction.PLATFORM_AUDIT_REFUND_ACCEPT.name(),
                    ERefundAction. PLATFORM_AUDIT_REFUND_REJECT.name()
            );
        }

        wrapper.eq(StringUtils.isNotEmpty(userUuid), "user_uuid",userUuid);

        wrapper.eq(StringUtils.isNotEmpty(orderItemUuid), "order_item_uuid",orderItemUuid);

    }


    public enum ERefundPhase {

        UN_SHIPPING_PHASE {
            @Override
            List<String> entityPhase() {
                return Arrays.asList(
                        ERefundAction.USER_REQUEST_REFUND_ON_LIMIT.name(),
                        ERefundAction.RETAILER_CANCEL_SHIPPED_ON_LIMIT.name()
                );
            }
        },
        AFTER_SHIPPING_PHASE {
            @Override
            List<String> entityPhase() {
                return Arrays.asList(
                        ERefundAction.USER_REQUEST_REFUND.name(),
                        ERefundAction.USER_CANCEL_REFUND.name(),
                        ERefundAction.PLATFORM_CANCEL_SHIPPED.name(),
                        ERefundAction.RETAILER_CANCEL_SHIPPED_AFTER_LIMIT.name(),
                        ERefundAction.PLATFORM_AUDIT_REFUND_ACCEPT.name(),
                        ERefundAction.PLATFORM_AUDIT_REFUND_REJECT.name()
                );
            }
        };
        abstract List<String> entityPhase();
    }



    @Getter
    public enum ERefundStateType {

        REFUNDED(ERefundState.COMPLETED.name()),

        REFUNDING(ERefundState.REFUND_REQUESTED.name()),

        REFUND_FAILED(
                     ERefundState.REJECTED.name(),
                     ERefundState.REFUND_FAILED.name()
        ),

        REFUND_CANCELED(ERefundState.USER_CANCELED.name());


        private String[] status;

        ERefundStateType(String... status) {
            this.status = status;
        }




    }


}

